﻿using BilbilDAL;
using Common;
using Modles.DTO;
using Modles.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BilbilBLL
{
    public class VideoPageFunctionService
    {
        //这里是视频页功能的业务逻辑层
        VideoInfoDAL videoInfoDAL = new VideoInfoDAL();
        VideoForwardDAL VideoForward = new VideoForwardDAL();
        VideoPageFuncitionDAL videoPageFuncitionDAL= new VideoPageFuncitionDAL();
        /// <returns></returns>
        /// <summary>
        /// 通过前端传过来的用户信息表id和视频信息表id给收藏表添加数据
        /// </summary>
        /// <returns></returns>
        public Response_DTO<string> AddtoCollection(AddCollection_DTO mod)
        {
            // 直接调用数据访问层方法，所有验证和业务逻辑都在DAL中处理
            return videoPageFuncitionDAL.AddtoCollection(mod);
        }
        /// <summary>
        /// 通过前端传过来的用户信息表id，视频信息表id和评论内容给视频评论表添加数据
        /// </summary>
        /// <returns></returns>
        public Response_DTO<string> AddVideoComment(AddComment_DTO mod)
        {
            Response_DTO<string> res = new Response_DTO<string>();
            // 参数验证
            if (mod.UserInfoId <= 0)
            {
                res.Success = false;
                res.Message = "用户信息错误";
                res.Conten = "NO";
                return res;
            }
            if (mod.VideoInfoId <= 0)
            {
                res.Success = false;
                res.Message = "视频信息错误";
                res.Conten = "NO";
                return res;
            }
            if (string.IsNullOrEmpty(mod.Content))
            {
                res.Success = false;
                res.Message = "评论内容不能为空";
                res.Conten = "NO";
                return res;
            }
            if (mod.Content.Length > 500)
            {
                res.Success = false;
                res.Message = "评论内容不能超过500个字符";
                res.Conten = "NO";
                return res;
            }
            // 调用数据访问层方法
            bool success = videoInfoDAL.AddVideoComment(mod.UserInfoId, mod.VideoInfoId, mod.Content);
            if (success)
            {
                res.Success = true;
                res.Message = "评论成功";
                res.Conten = "YES";
            }
            else
            {
                // 根据实际情况可以进一步细化错误信息
                res.Success = false;
                res.Message = "评论失败，请稍后再试";
                res.Conten = "NO";
            }
            return res;
        }

        /// <summary>
        /// 通过前端传过来的用户信息表id和视频信息表id给视频转发表添加数据
        /// </summary>
        /// <param name="mod_DTO"></param>
        /// <returns></returns>
        public ResponseTool AddVideoForward(VideoForward_DTO mod_DTO)
        {
            try
            {
                
                if (mod_DTO.UserInfoId <= 0)
                {
                    return ResponseTool.Fail("用户不存在");
                }
                if (mod_DTO.VideoInfoId <= 0)
                {
                    return ResponseTool.Fail("视频不存在");
                }
                var success = VideoForward.AddVideoForward(mod_DTO);
                if (success)
                {
                    return ResponseTool.Succeed("转发成功");
                }
                else
                {
                    return ResponseTool.Succeed("转发失败");
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 根据视频ID获取评论数据
        /// </summary>
        /// <param name="videoInfoId">视频信息表ID</param>
        /// <returns>评论数据列表</returns>
        public Response_DTO<List<CommentInfo_DTO>> GetCommentsByVideoId(int videoInfoId)
        {
            // 直接调用数据访问层方法获取评论数据
            return videoInfoDAL.GetCommentsByVideoId(videoInfoId);
        }
        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseTool AddVideoLike(int userInfoId, int videoInfoId)
        {
            if (userInfoId == 0)
            {
                return ResponseTool.Fail("用户Id不能为空");
            }
            if (videoInfoId == 0)
            {
                return ResponseTool.Fail("视频Id不能为空");
            }
            

            if(new VideoLikeDAL().AddVideoLike(userInfoId, videoInfoId))
            {
                return ResponseTool.Succeed("点赞成功");
            } else
            {
                return ResponseTool.Fail("点赞失败");
            }
            
        }
        /// <summary>
        /// 取赞
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseTool DeleteVideoLike(int userInfoId, int videoInfoId)
        {
            if (userInfoId == 0)
            {
                return ResponseTool.Fail("用户Id不能为空");
            }
            if (videoInfoId == 0)
            {
                return ResponseTool.Fail("视频Id不能为空");
            }
            
            if (new VideoLikeDAL().DeleteVideoLike(userInfoId, videoInfoId))
            {
                return ResponseTool.Succeed("取赞成功");
            }
            else
            {
                return ResponseTool.Fail("取赞失败");
            }
        }
        /// <summary>
        /// 根据视频id获取视频信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public ResponseTool GetVideoInfoById(int userInfoId, int videoInfoId)
        {
            if (userInfoId == 0)
            {
                return ResponseTool.Fail("用户信息Id不能为空");
            }
            if (videoInfoId == 0)
            {
                return ResponseTool.Fail("视频信息Id不能为空");
            }

            VideoInfo_DTO videoInfo_DTO = new VideoInfoDAL().GetVideoInfoById(userInfoId, videoInfoId);
            if (videoInfo_DTO != null)
            {
                return ResponseTool.Succeed(videoInfo_DTO);
            } else
            {
                return ResponseTool.Fail("没有该视频");
            }
        }
        /// <summary>
        /// 获取视频点赞收藏转发量
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public ResponseTool GetVideoThreeClick(int userInfoId, int videoInfoId)
        {
            if(userInfoId == 0)
            {
                return ResponseTool.Fail("用户Id不能为空");
            }
            if (videoInfoId == 0)
            {
                return ResponseTool.Fail("视频Id不能为空");
            }
            VideoThreeClick_DTO videoThreeClick_DTO = new VideoThreeClick_DTO();
            videoThreeClick_DTO.Like = new VideoLikeDAL().GetVideoLike(userInfoId, videoInfoId, out int LikeCount);
            videoThreeClick_DTO.LikeCount = LikeCount;
            videoThreeClick_DTO.Collection = new CollectionDAL().GetCollection(userInfoId, videoInfoId, out int CollectionCount);
            videoThreeClick_DTO.CollectionCount = CollectionCount;
            videoThreeClick_DTO.Share = new VideoForwardDAL().GetVideoForwardCount(videoInfoId);

            return ResponseTool.Succeed(videoThreeClick_DTO);
        }
        /// <summary>
        /// 添加收藏
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public ResponseTool AddCollectionInfo(int userInfoId, int videoInfoId)
        {
            if (userInfoId == 0)
            {
                return ResponseTool.Fail("用户Id不能为空");
            }
            if (videoInfoId == 0)
            {
                return ResponseTool.Fail("视频Id不能为空");
            }

            if(new CollectionDAL().AddCollectionInfo(userInfoId, videoInfoId))
            {
                return ResponseTool.Succeed("收藏成功");
            }
            else
            {
                return ResponseTool.Fail("收藏失败");
            }
        }
        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public ResponseTool DeleteCollectionInfo(int userInfoId, int videoInfoId)
        {
            if (userInfoId == 0)
            {
                return ResponseTool.Fail("用户Id不能为空");
            }
            if (videoInfoId == 0)
            {
                return ResponseTool.Fail("视频Id不能为空");
            }

            if (new CollectionDAL().DeleteCollectionInfo(userInfoId, videoInfoId))
            {
                return ResponseTool.Succeed("取消收藏成功");
            }
            else
            {
                return ResponseTool.Fail("取消收藏失败");
            }
        }
        /// <summary>
        /// 获取评论列表
        /// </summary>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public ResponseTool GetVideoComments(int videoInfoId)
        {
            if(videoInfoId <= 0)
            {
                return ResponseTool.Fail("视频Id不能为空");
            }
            return ResponseTool.Succeed(new CommentsDAL().GetVideoComments(videoInfoId));
        }
        /// <summary>
        /// 发布视频评论
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public ResponseTool AddVideoComment(int userInfoId, int videoInfoId, string content)
        {
            if (userInfoId == 0)
            {
                return ResponseTool.Fail("用户Id不能为空");
            }
            if (videoInfoId == 0)
            {
                return ResponseTool.Fail("视频Id不能为空");
            }
            if (content == null)
            {
                return ResponseTool.Fail("评论内容不能为空");
            }

            if (new CommentsDAL().AddVideoComment(userInfoId, videoInfoId, content))
            {
                return ResponseTool.Succeed("发布评论成功");
            } else
            {
                return ResponseTool.Fail("发布评论失败");
            }
        }
    }
}
